package com.example.bookstore.security

import com.example.bookstore.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.stereotype.Service

/**
 * Created by IntelliJ IDEA.
 * User: lvshe
 * Date: 2019/8/13
 * Time: 23:07
 */
@Service
class MyUserDetailsService : UserDetailsService {

    @Autowired
    private lateinit var userService: UserService

    override fun loadUserByUsername(username: String): UserDetails {
        val user = userService.findByEmailOrNull(username) ?: throw SecurityException("用户无法找到 UserDetailsService")
        return JwtUser.createJwtUser(user)
    }
}